from . import admin_blue
from flask import render_template, request, session, redirect, url_for, current_app
from info import models
from info.utils.common import check_islogin
import time
from datetime import datetime,timedelta


# 登录
@admin_blue.route("/login", methods=["POST", "GET"])
def admin_login():
    # 第一次请求判断是否是gei还是post
    if request.method == "GET":
        user_id = session.get('user_id')
        is_admin = session.get('is_admin')
        # 管理员在前台已经登录
        if user_id and is_admin:
            return redirect(url_for('admin_blue.admin_index'))
        return render_template("admin/login.html")
    #　获取参数
    userName = request.form.get("username")
    userPwd = request.form.get("password")
    # 验证
    if not all([userName, userPwd]):
        return render_template("admin/login.html", errno="参数错误")
    user = models.User.query.filter(models.User.is_admin==True, models.User.mobile==userName).first()
    if not user:
        return render_template("admin/login.html", errno="请登录")
    if not user.check_password(userPwd):
        return render_template("admin/login.html", errno="密码错误")
    # 做状态保持
    session["user_id"] = user.id
    session["mobile"] = user.mobile
    session["nick_name"] = user.nick_name
    session["is_admin"] = user.is_admin
    # return render_template("admin/index.html", errno="OK")
    return redirect(url_for('admin_blue.admin_index'))


# 展示首页
@admin_blue.route("/index")
def admin_index():
    user_id = session.get("user_id")
    user = models.User.query.get(user_id)
    if not user:
        return redirect("/admin/login.html")

    data = {
        "user_info": user.to_dict()
    }
    return render_template("admin/index.html", data=data)

# 用户统计
@admin_blue.route("/user_count")
def user_count():
    # 总人数
    total_count = 0
    try:
        total_count = models.User.query.filter(models.User.is_admin == False).count()
    except Exception as e:
        current_app.logger.error(e)

    #获取当前时间
    now_time = time.localtime()
    mon_begin = "%d-%02d-1" % (now_time.tm_year, now_time.tm_mon)
    mon_begin_day = datetime.strptime(mon_begin, "%Y-%m-%d")
    # 月增人数
    mon_count = 0
    try:
        mon_count = models.User.query.filter(models.User.is_admin == False, models.User.create_time >= mon_begin_day).count()
    except Exception as e:
        current_app.logger.error(e)
    # 日增加人数
    day_begin = "%d-%02d-%02d" % (now_time.tm_year, now_time.tm_mon, now_time.tm_mday)
    day_begin_day = datetime.strptime(day_begin, "%Y-%m-%d")
    day_count = 0
    try:
        day_count = models.User.query.filter(models.User.is_admin == False, models.User.create_time >= day_begin_day).count()
    except Exception as e:
        current_app.logger.error(e)

    # 图表显示数据
    activate_count = []
    activate_time = []


    data ={
        "total_count": total_count,
        "mon_count": mon_count,
        "day_count": day_count,
        "active_count": activate_count,
        "active_date": activate_time,
    }
    return render_template("admin/user_count.html", data=data)


